<template>
  <div class="container" style="height: 100%">
    <div class="flex_cm" style="height: 100%">
      <search
        :data="header_list"
        @changeIsShow="
          (e) => {
            header_list[e].isShow = !header_list[e].isShow
            header_list[e].value =
              header_list[e].type == 'input_number' ? ['', ''] : ''
          }
        "
        @search="search_key"
        @reset="
          header_list.forEach((item) => {
            item.value = ''
          }),
            search_key()
        "
      ></search>
      <div class="p_0_10 bg_fff flex1">
        <div class="flex_js p_10_0">
          <div class="c_666 fw">表单列表</div>
          <div class="flex">
            <el-button
              type="primary"
              @click="show_box = true"
              v-if="has_role_list[0].has_role"
              >新增</el-button
            >
            <el-button type="primary" v-if="has_role_list[0].has_role"
              >配置</el-button
            >
            <setTable
              :data="header_list"
              :height_size="height_size"
              @changeShowTable="
                (e) => {
                  header_list[e].showTable = !header_list[e].showTable
                }
              "
              @resetTableSet="
                () => {
                  header_list.forEach((item) => {
                    item.showTable = true
                  })
                }
              "
              @changeHeightSize="
                (e) => {
                  height_size = e
                }
              "
            ></setTable>
          </div>
        </div>
        <el-table
          style="text-align: center"
          :data="list"
          :max-height="max_height"
          border
          v-loading="loading"
          :default-sort="{ prop: 'createTime' }"
          :row-style="{
            height: height_size == 2 ? '50px' : '',
          }"
          :cell-style="{
            padding: height_size == 3 ? '0' : '',
          }"
        >
          <el-table-column
            type="index"
            label="序号"
            width="80px"
          ></el-table-column>
          <el-table-column
            label="表单类型"
            key="表单类型"
            sortable
            v-if="header_list[0].showTable"
          >
            <template #default="scope">
              <div v-for="item in form_type_list" :key="item.id">
                <span v-if="item.dictCode == scope.row.type">{{
                  item.dictValue
                }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="表单名称"
            prop="name"
            key="表单名称"
            sortable
            v-if="header_list[1].showTable"
          ></el-table-column>
          <el-table-column
            label="表单代号"
            prop="code"
            key="表单代号"
            sortable
            v-if="header_list[2].showTable"
          ></el-table-column>
          <!-- <el-table-column
            label="所属模块"
            key="所属模块"
            sortable
            prop="mould"
            v-if="header_list[2].showTable"
          >
          </el-table-column> -->
          <!-- <el-table-column
            label="所属菜单"
            prop="menuLocation"
            key="所属菜单"
            sortable
            v-if="header_list[1].showTable"
          ></el-table-column> -->
          <el-table-column
            label="状态"
            key="状态"
            sortable
            prop="isDelete"
            v-if="header_list[3].showTable"
            ><template #default="scope: any">
              <div v-if="scope.row.status" style="color: #3580eb">已启用</div>
              <div v-else>已停用</div></template
            ></el-table-column
          >
          <el-table-column label="操作" v-if="has_role_list[0].has_role">
            <template #default="scope: any">
              <el-button
                type="primary"
                @click="
                  router.push({
                    name: 'customFormSetup',
                    query: { name: scope.row.name, id: scope.row.id },
                  })
                "
                v-if="has_role_list[0].has_role"
                >配置</el-button
              >
              <el-button @click="edit_list(scope)">编辑</el-button>
              <!-- <el-button @click="edit_list(scope)">停用</el-button>
              <el-button @click="edit_list(scope)">启用</el-button> -->
              <el-button
                v-if="!scope.row.isDelete"
                @click="delete_list(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-drawer
      v-model="show_box"
      direction="rtl"
      size="70%"
      destroy-on-close
      @close="resetForm(addFormform)"
      :close-on-click-modal="false"
    >
      <template #title>
        <h4 v-if="add_form_data.id == ''">新增项目</h4>
        <h4 v-else>编辑项目</h4>
      </template>
      <template #default>
        <el-form
          ref="addFormform"
          :model="add_form_data"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="表单类型" prop="type">
            <el-select v-model="add_form_data.type">
              <el-option
                v-for="item in form_type_list"
                :key="item.id"
                :value="item.dictCode"
                :label="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="表单名称" prop="name">
            <el-input
              v-model="add_form_data.name"
              placeholder="请输入"
              show-word-limit
              maxlength="30"
            ></el-input>
          </el-form-item>
          <el-form-item label="表单代号" prop="code">
            <el-input
              v-model="add_form_data.code"
              placeholder="请输入"
              show-word-limit
              maxlength="30"
            ></el-input>
          </el-form-item>
          <!-- <el-form-item label="所属菜单">
            <el-cascader
              v-model="add_form_data.menu"
              :options="options"
              @change="change_menu"
            >
            </el-cascader>
          </el-form-item> -->
          <div class="tr m_top10">
            <el-button @click="resetForm(addFormform)">取消</el-button>
            <el-button
              @click="submit_add(addFormform)"
              type="primary"
              :loading="btn_loading"
              >确认</el-button
            >
          </div>
        </el-form>
      </template>
    </el-drawer>
  </div>
</template>
<script lang="ts" setup>
import {
  check_role,
  debounce,
  dictData,
  handle_search_key,
} from '@/utils/utils' // 权限校验
import search from '@/view/components/search.vue'
import setTable from '@/view/components/setTable.vue'
import { onMounted, ref, onUnmounted, computed } from 'vue'
import axios from '@/service/HttpRequest'
import serviceApi from '@/service/api'
import EventBus from '@/utils/EventBus'
import { FormInstance, ElMessage, ElMessageBox } from 'element-plus'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
const store = useStore()
const router = useRouter()
const has_role_list = ref([{ path: 'szlsProject/post', has_role: false }])
// type FormInstance = InstanceType<typeof ElForm>
const addFormform = ref<FormInstance>()
const rules = ref({
  // 表单验证规则
  name: [{ required: true, message: '请输入表单名称', trigger: 'blur' }],
  code: [{ required: true, message: '请输入表单编号', trigger: 'blur' }],
  type: [{ required: true, message: '请输入表单类型', trigger: 'change' }],
})
const list = ref()
const loading = ref(false)
const show_box = ref(false)
const height_size = ref(1)
const header_list = ref([
  {
    label: '表单类型',
    value: '',
    showTable: true,
    type: 'select',
    isShow: true,
    options: computed(() => {
      return form_type_list.value
    }),
  },
  {
    label: '表单名称',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '表单代号',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '状态',
    value: '',
    showTable: true,
    type: 'select',
    isShow: false,
    options: [
      {
        value: '0',
        label: '进行中',
      },
      {
        value: '1',
        label: '回收站',
      },
    ],
  },
])
const page_data = ref({
  name: '',
  status: '',
  type: '',
  code: '',
})
const add_form_data = ref({
  id: '',
  name: '',
  status: true,
  type: '',
  code: '',
})
const form_type_list = ref()
const max_height = ref()
onMounted(() => {
  max_height.value = window.innerHeight - 320 // 带分页的 table 高度
  EventBus.on('refresh', () => {
    get_list()
    ElMessage.success('刷新成功！')
  })
  has_role_list.value.forEach((item) => {
    item.has_role = check_role(item)
    item.has_role = true
  })
  dictData('1664101580288299009').then((res) => {
    form_type_list.value = res as any
  })
  get_list()
})
onUnmounted(() => {
  EventBus.off('refresh')
})
const delete_list = (e) => {
  ElMessageBox.confirm('确定删除该记录？', '提示', {
    cancelButtonText: '取消',
    confirmButtonText: '确定',
    type: 'warning',
  })
    .then(() => {
      axios
        .request({ url: serviceApi.form_curd + '/' + e.id, method: 'delete' })
        .then(() => {
          ElMessage.success('操作成功！')
          get_list()
        })
        .catch((err) => {
          ElMessage.error(err)
        })
    })
    .catch(() => {})
}
const edit_list = (e) => {
  add_form_data.value = {
    id: e.row.id,
    name: e.row.name,
    status: e.row.status,
    // menuLocation: e.row.menuLocation,
    type: e.row.type + '',
    // mould: e.row.mould,
    code: e.row.code,
    // menu: [e.row.mould, e.row.code.replace('/', '')],
  }
  console.log(add_form_data.value)
  show_box.value = true
}
const btn_loading = ref(false)
const submit_add = async (formEl: FormInstance | undefined) => {
  await formEl.validate((valid) => {
    if (valid) {
      if (btn_loading.value) return
      btn_loading.value = true
      axios
        .request({
          url:
            add_form_data.value.id == ''
              ? serviceApi.form_curd
              : serviceApi.form_curd + '/' + add_form_data.value.id,
          method: add_form_data.value.id == '' ? 'post' : 'put',
          data: add_form_data.value,
        })
        .then(() => {
          ElMessage.success('操作成功！')
          setTimeout(() => {
            btn_loading.value = false
            resetForm(formEl)
            get_list()
            show_box.value = false
          }, 1000)
        })
        .catch((err) => {
          ElMessage.error(err)
          btn_loading.value = false
        })
    }
  })
}
const get_list = () => {
  loading.value = true
  axios
    .request({
      url: serviceApi.form_list,
      method: 'post',
      data: page_data.value,
    })
    .then((res) => {
      console.log(res)
      list.value = res.data
      loading.value = false
    })
    .catch((err) => {
      ElMessage.error(err)
      loading.value = false
    })
}
const search_key = () => {
  console.log(header_list.value)
  page_data.value.type = header_list.value[0].value
  page_data.value.name = header_list.value[1].value
  page_data.value.code = header_list.value[2].value
  //   page_data.value.enterpriseName = header_list.value[2].value
  //   page_data.value.mould = header_list.value[2].value
  //   page_data.value.menuLocation = header_list.value[3].value
  page_data.value.status = header_list.value[3].value
  get_list()
}
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  add_form_data.value = {
    id: '',
    name: '',
    status: true,
    // menuLocation: '',
    type: '',
    code: '',
    // mould: '',
    // menu: [],
  }
  show_box.value = false
}
</script>

<style lang="scss" scoped></style>
